import axios from 'axios'
import qs from 'qs'

const URLS = {
    types:'http://localhost/filmApi/loadTypes.php',
    films:'http://localhost/filmApi/loadFilms.php'
}

const loadTypes = function(){
    // 返回Promise对象
    return axios.get(URLS.types)
        .then(res => {
            if(res.data.resultState){
                return res.data.result
            }
            return Promise.reject({
                message:res.data.message
            })
        })
}

const loadFilmsByParams = function(params={}){
    return axios.post(URLS.films,qs.stringify(params))
        .then(res => {
            if(res.data.resultState){
                return res.data.result
            }
            return Promise.reject({
                message:res.data.message
            })
        })
}

export default {
    install(Vue){
        Vue.prototype.$api = {
            loadTypes,
            loadFilmsByParams
        }
    }
}